using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace WindowsPhoneGame1
{
    class Camera2D : GameComponent
    {

        protected float _scale;
        protected Vector2 _position;
        protected float _viewportHeight;
        protected float _viewportWidth;


        public Camera2D(Game game)
            : base(game)
        {
            Initialize();
        }

        public override void Initialize()
        {
            _viewportWidth = GraphicsDeviceManager.DefaultBackBufferWidth;
            _viewportHeight =GraphicsDeviceManager.DefaultBackBufferHeight;

            ScreenCenter = new Vector2(_viewportWidth / 2, _viewportHeight / 2);
            Scale = 1;
            MoveSpeed = 1.25f;

            base.Initialize();
        }


        public Vector2 ScreenCenter
        {
            get;
            protected set;
        }

        public float MoveSpeed { get; set; }

        public Focusable Focus { get; set; }

        public void Update(int delta)
        {
           // delta = 50;
            Transform = Matrix.Identity *
                        Matrix.CreateTranslation(-Position.X, -Position.Y, 0)
                       *Matrix.CreateRotationZ(Rotation) *
                        Matrix.CreateTranslation(Origin.X, Origin.Y, 0)

                // Matrix.CreateScale(new Vector3(Scale, Scale, 0))
                        ;
            
            Origin = ScreenCenter ;
           // Origin = new Vector2(800 / 2, 480 / 2);
            //Transform = Matrix.Identity;
            //
            if (Focus != null)
            {
                _position.X += (Focus.Position.X - Position.X) *MoveSpeed;
                _position.Y += (Focus.Position.Y - Position.Y) *MoveSpeed;
                //_position.X = (Focus.Position.X ) ;
                //_position.Y  =(Focus.Position.Y) ;
            }

        }

        public Matrix Transform
        {
            get;
            set;
        }

        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
            }
        }

        public float Rotation
        {
            get;
            set;
        }

        public float Scale
        {
            get
            {
                return _scale;
            }
            set
            {
                if (value > 1 || value <= 0)
                    throw new Exception("scale must in 0~1");
                _scale = value;
            }
        }

        public Vector2 Origin
        {
            get;
            set;
        }


    }
}
